﻿using Modules.System.Area;
using PmSoft;

namespace Modules.System.Dept
{
    public static partial class DeptExceptions
    {
        /// <summary>
        /// 获取单位归属地完整名称
        /// </summary>
        /// <param name="dept"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetFullAreaName(this SysDept dept, string defaultValue = "N/A")
        {
            var areaService = ServiceAccessor.GetService<AreaService>();
            return areaService.GetAreaFullName(dept.ProvinceCode, dept.CityCode, dept.AreaCode, defaultValue);
        }

        /// <summary>
        /// 获取单位归属省份名称
        /// </summary>
        /// <param name="dept"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetProvinceName(this SysDept dept, string defaultValue = "N/A")
        {
            if (!dept.ProvinceCode.HasValue)
                return defaultValue;

            var areaService = ServiceAccessor.GetService<AreaService>();
            var area = areaService.GetArea(dept.ProvinceCode.Value);
            return area == null ? defaultValue : area.Name;
        }

        /// <summary>
        /// 获取单位归属城市名称
        /// </summary>
        /// <param name="dept"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetCityName(this SysDept dept, string defaultValue = "N/A")
        {
            if (!dept.CityCode.HasValue)
                return defaultValue;

            var areaService = ServiceAccessor.GetService<AreaService>();
            var area = areaService.GetArea(dept.CityCode.Value);
            return area == null ? defaultValue : area.Name;
        }

        /// <summary>
        /// 获取单位归属区县名称
        /// </summary>
        /// <param name="dept"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetAreaName(this SysDept dept, string defaultValue = "N/A")
        {
            if (!dept.AreaCode.HasValue)
                return defaultValue;

            var areaService = ServiceAccessor.GetService<AreaService>();
            var area = areaService.GetArea(dept.AreaCode.Value);
            return area == null ? defaultValue : area.Name;
        }
    }
}